package com.jxtc.enterprise.delivery.controller;

import com.alibaba.fastjson.JSONObject;
import com.jxtc.enterprise.common.utils.Result;
import com.jxtc.enterprise.common.utils.ResultGenerator;
import com.jxtc.enterprise.common.vo.PaginationResponseVO;
import com.jxtc.enterprise.delivery.service.RiderHistoryDataService;
import com.jxtc.enterprise.delivery.vo.historyData.DailyTaskIncomeDetailVO;
import com.jxtc.enterprise.delivery.vo.historyData.DailyTaskIncomeVO;
import com.jxtc.enterprise.delivery.vo.historyData.InComeSummaryVO;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 配送员 H5 "历史数据" 页面相关接口
 */
@RestController
@RequestMapping("/jxtc/enterprise/delivery/data")
@RequiredArgsConstructor
@Slf4j
public class RiderHistoryDataController {

    private final RiderHistoryDataService riderHistoryDataService;


    /**
     * 接口 6.3.1 查看总收入
     *
     * @param riderId 配送员 ID
     * @return 累计总收入 + 本月总收入
     */
    @GetMapping("/queryInComeSummary")
    public Result<InComeSummaryVO> queryInComeSummary(@RequestParam(value = "riderId") String riderId) {
        log.info("/queryInComeSummary, riderId: {}", riderId);
        InComeSummaryVO vo = riderHistoryDataService.queryInComeSummary(riderId);
        log.info("/queryInComeSummary, response: {}", JSONObject.toJSONString(vo));
        return ResultGenerator.genSuccessResult(vo);
    }


    /**
     * 接口 6.3.2 分页查看每天配送任务数及佣金
     *
     * @param riderId  配送员 ID
     * @param pageNo   页码，默认从 1 开始
     * @param pageSize 每页条数，默认值为 20
     * @return 每日配送任务数及佣金列表
     */
    @GetMapping("/queryDailyTaskAndIncomeByPage")
    public Result<PaginationResponseVO<DailyTaskIncomeVO>> queryDailyTaskAndIncomeByPage(@RequestParam(value = "riderId") String riderId,
                                                                                         @RequestParam(value = "pageNo", required = false, defaultValue = "1") Integer pageNo,
                                                                                         @RequestParam(value = "pageSize", required = false, defaultValue = "20") Integer pageSize) {
        log.info("/queryDailyTaskAndIncomeByPage, riderId: {}, pageNo: {}, pageSize: {}", riderId, pageNo, pageSize);
        PaginationResponseVO<DailyTaskIncomeVO> vo = riderHistoryDataService.queryDailyTaskAndIncomeByPage(riderId, pageNo, pageSize);
        log.info("/queryDailyTaskAndIncomeByPage, response: {}", JSONObject.toJSONString(vo));
        return ResultGenerator.genSuccessResult(vo);
    }


    /**
     * 接口 6.3.3 查看某天配送任务及收入详情
     *
     * @param riderId 配送员 ID
     * @param date    配送日期
     * @return 每日配送任务及收入详情
     */
    @GetMapping("/queryDailyTaskAndIncomeDetail")
    public Result<List<DailyTaskIncomeDetailVO>> queryDailyTaskAndIncomeDetail(@RequestParam("riderId") String riderId, @RequestParam("date") String date) {
        log.info("/queryDailyTaskAndIncomeDetail, riderId: {}, date: {}", riderId, date);
        List<DailyTaskIncomeDetailVO> voList = riderHistoryDataService.queryDailyTaskAndIncomeDetail(riderId, date);
        log.info("/queryDailyTaskAndIncomeDetail, response: {}", JSONObject.toJSONString(voList));
        return ResultGenerator.genSuccessResult(voList);
    }


    /**
     * 接口 6.3.4 导出某月的配送任务详情
     *
     * @param riderId 配送员 ID
     * @param month   月份，格式：yyyy-MM
     */
    @GetMapping("/exportByMonth")
    public void exportByMonth(@RequestParam("riderId") String riderId, @RequestParam("month") String month, HttpServletResponse response) {
        log.info("/exportByMonth, riderId: {}, month: {}", riderId, month);
        riderHistoryDataService.exportByMonth(riderId, month, response);
    }
}
